import cookie from '@/utils/store/cookie.js'

import {
	login,
	getUserInfo,
	getUserInfoByOpenid,
	serverLogin,
} from '@/api/user'

let state = {
	hasLogin: false || (uni.getStorageSync('openId') != ""),
	token: '',
	openId: '',
	session_key: '',
	userinfo: {},
	nickname: '',
	avatar: '',
	deviceType: 1
}

const mutations = {
	SET_HAS_LOGIN: (state, hasLogin) => {
		state.hasLogin = hasLogin
	},
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_OPENID: (state, openId) => {
		state.openId = openId
	},
	SET_SESSION_KEY: (state, session_key) => {
		state.session_key = session_key
	},
	SET_USERINFO: (state, userinfo) => {
		state.userinfo = userinfo
	},
	SET_NICKNAME: (state, nickname) => {
		state.nickname = nickname
	},
	SET_AVATAR: (state, avatar) => {
		state.avatar = avatar
	},
	SET_DEVICETYPE: (state, deviceType) => {
		state.deviceType = deviceType
	}
}

const actions = {
	//  #ifdef MP
	saveOpenId({
		commit
	}, data) {
		const {
			openId
		} = data
		return new Promise((resolve, reject) => {
			uni.setStorageSync('openId', openId);
			commit('SET_OPENID', openId);

			resolve()
		})
	},
	// #endif
	login({
		commit
	}, data) {
		const {
			openId,
			inviteOpenId,
			inviteUserId
		} = data
		return new Promise((resolve, reject) => {

			if (openId === "") {
				reject('openId is null.')
				return;
			} else {

				const parmas = {
					openId: openId,
					inviteOpenId: inviteOpenId,
					inviteUserId: inviteUserId
				}
				serverLogin(parmas).then(async ({
					data
				}) => {
					if (!data) {
						reject('Verification failed, please Login again.')
						return
					}
					const {
						id,
						token,
					} = data
					// 测试账号openId：

					uni.setStorage({
						key: 'openId',
						data: openId
					})
					uni.setStorage({
						key: 'userId',
						data: id
					})
					uni.setStorage({
						key: 'token',
						data: token
					})

					commit('SET_HAS_LOGIN', true);
					commit('SET_OPENID', openId);
					commit('SET_TOKEN', token);

					resolve()

				})
			}


		})
	},
	/**
	 * 设置token
	 */
	setToken({
		commit
	}, data) {
		const {
			token
		} = data

		return new Promise((resolve, reject) => {
			commit('SET_TOKEN', token);
			resolve()
		})
	},

	/**
	 * 选取昵称
	 */
	chooseUsername({
		commit
	}, data) {
		const {
			nickname
		} = data

		return new Promise((resolve, reject) => {
			commit('SET_NICKNAME', nickname)
			resolve()
		})
	},

	/**
	 * 选取头像
	 */
	chooseAvatar({
		commit
	}, data) {
		const {
			avatar
		} = data

		return new Promise((resolve, reject) => {
			commit('SET_AVATAR', avatar)
			resolve()
		})
	},
	// 获取用户信息
	getUserInfo({
		commit
	}, data) {
		const {
			openId
		} = data
		return new Promise((resolve, reject) => {

			if (openId === "") {
				reject('openId is null.')
				return;

			} else {

				getUserInfoByOpenid(openId).then(async ({
					data
				}) => {
					if (!data) {
						reject('Verification failed, please Login again.')
					}
					const {
						userName
					} = data

					commit('SET_NICKNAME', userName)

					resolve(data)
				}).catch(error => {
					reject(error)
				})
			}

		})
	},
	/**
	 * 清空
	 */
	clearAll({
		commit
	}, data) {

		return new Promise((resolve, reject) => {

			cookie.clearAll()
			uni.setStorageSync('hasLogin', "");

			commit('SET_USERINFO', {})
			resolve()
		})
	},

	// user logout
	logout({
		commit,
		state,
	}) {

		return new Promise((resolve, reject) => {

			console.log('logout')

			cookie.clearAll()

			uni.removeStorageSync("userInfo")
			uni.removeStorageSync("token")
			uni.removeStorageSync('hasLogin')

			uni.removeStorageSync("openId")

			commit('SET_USERINFO', {})
			commit('SET_HAS_LOGIN', false)
			commit('SET_NICKNAME', '')
			commit('SET_AVATAR', '')

			resolve()

		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}